package alo.p10.work;

import alo.p10.HeapSort;

/**
 * @ClassName FindTwoSumIsK
 * @Description 判断两个数组中是否有存在两数之和为K
 * @Author Fanxc
 * @DATE 2021/3/12 14:57
 * @Version 1.0
 */
public class FindTwoSumIsK {
    boolean findTwoSumIsK(int[] A,int[] B,int n,int K){
        int c;
        new HeapSort().heapSort(A,0,n-1);
        for (int i = 0; i < n; i++) {
            c=K-B[i];
            if (binarySearch(A,c)){
                return true;
            }
        }
        return false;
    }


    public boolean binarySearch(int[] a,int key){
        int lo=0,hi=a.length-1;
        while (lo<=hi){
            int mid=lo+(hi-lo)/2;
            if (key<a[mid]){
                hi=mid-1;
            }else if (key>a[mid]){
                lo=mid+1;
            }else return true;
        }
        return false;
    }
}
